﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DPW.Business.Common;
using DPW.Business.Repository;
using DPW.Models;

namespace DPW.Controllers
{
    public class PoemController : Controller
    {
        //
        // GET: /Poem/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Poem/Details/5

        public ActionResult Details(int id)
        {

            var model = new PoemDetailViewModel();
            model.Poem = PoemRepository.GetPoemById(id);
            model.Category = model.Poem.Category;
            model.OlderPoems = PoemRepository.GetOlderPoems(model.Poem);
            model.NewerPoems = PoemRepository.GetNewerPoems(model.Poem);
            model.Categories = CategoryRepository.GetAllCategories();
            model.LatestPoems = PoemRepository.GetLatestPoems(10);
            model.MostViewedPoems = PoemRepository.GetMostViewedPoems(5);

            model.Poem.Views += 1;
            model.Poem.Update();

            return View(model);
        }

        //
        // GET: /Poem/Create

        public ActionResult Create()
        {
            // Prepare viewmodel
            var poemCreateViewModel = new PoemCreateViewModel();
            poemCreateViewModel.Categories = CategoryRepository.GetAllCategories();


            return View(poemCreateViewModel);
        }

        //
        // POST: /Poem/Create

        [HttpPost]
        [ValidateInputAttribute(false)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                string poemName = collection[Constants.Poem.Name];
                string poemContent = collection[Constants.Poem.Content];
                poemContent = HttpUtility.HtmlEncode(poemContent);
                string categoryId = collection["Category"];

                int catId = 0;
                int.TryParse(categoryId, out catId);

                PoemRepository.AddNewPoem(poemName, "", poemContent, "", catId);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Poem/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Poem/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Poem/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Poem/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
